<template>
  <div class="login-container">
    <el-form
      class="login-form"
      :model="loginForm"
      :rules="loginRules"
      ref="loginForm"
    >
      <div class="title-container">
        <h3 class="title">{{ title }}</h3>
      </div>
      <el-form-item prop="username">
        <span class="svg-container">
          <svg-icon icon="user"></svg-icon>
        </span>
        <el-input
          v-model.trim="loginForm.username"
          placeholder="请输入用户名"
        ></el-input>
      </el-form-item>
      <el-form-item prop="password">
        <span class="svg-container">
          <svg-icon icon="password"></svg-icon>
        </span>
        <el-input
          v-model.trim="loginForm.password"
          :type="passwordType1"
          placeholder="请输入密码"
        ></el-input>
        <span class="show-pwd">
          <svg-icon @click.native="changePwdType1" :icon="passwordType1 == 'password' ? 'eye' : 'eye-open'"></svg-icon>
        </span>
      </el-form-item>
      <el-form-item v-if="btn1 == '注册'" prop="repassword">
        <span class="svg-container">
          <svg-icon icon="password"></svg-icon>
        </span>
        <el-input
          v-model.trim="loginForm.repassword"
          :type="passwordType2"
          placeholder="请再次确认密码"
        ></el-input>
        <span class="show-pwd">
          <svg-icon @click.native="changePwdType2" :icon="passwordType2 == 'password' ? 'eye' : 'eye-open'"></svg-icon>
        </span>
      </el-form-item>

      <!-- 登录按钮 -->
      <el-button
        type="primary"
        @click="changeHandleClick"
        style="width: 100%; margin-bottom: 30px"
        >{{ btn1 }}</el-button
      >
      <br />
      <!-- 登录注册切换按钮 -->
      <el-button @click="changeBtn" style="width: 100%; margin-bottom: 30px">{{
        btn2
      }}</el-button>
    </el-form>
  </div>
</template>

<script>
import { regUser } from "../../api/user";
export default {
  data() {
    return {
      title: "登录",
      btn1: "登录",
      btn2: "切换为注册",
      loginForm: {
        username: "",
        password: "",
        repassword: "",
      },
      loginRules: {
        username: [
          { required: true, message: "username is required", trigger: "blur" },
        ],
        password: [
          { required: true, message: "password is required", trigger: "blur" },
        ],
        repassword: [
          {
            required: true,
            message: "repassword is required",
            trigger: "blur",
          },
        ],
      },
      passwordType1: "password",
      passwordType2: "password",
    };
  },
  mounted() {},
  methods: {
    changeBtn() {
      if (this.btn2 == "切换为注册") {
        this.title = "注册";
        this.btn1 = "注册";
        this.btn2 = "切换为登录";
        this.loginForm = {
          username: "",
          password: "",
          repassword: "",
        };
      } else {
        this.title = "登录";
        this.btn1 = "登录";
        this.btn2 = "切换为注册";
        this.loginForm = {
          username: "",
          password: "",
          repassword: "",
        };
      }
    },
    changeHandleClick() {
      if (this.btn1 == "登录") {
        this.login();
      } else if (this.btn1 == "注册") {
        this.regUser();
      }
    },
    // 登录
    login() {
      this.$refs["loginForm"].validate((valid) => {
        if (valid) {
          this.$store.dispatch("user/login", {
            username: this.loginForm.username,
            password: this.loginForm.password,
          });
        } else {
          this.$message.error("用户名或密码不能为空");
        }
      });
    },
    // 注册
    regUser() {
      this.$refs["loginForm"].validate(async (valid) => {
        if (valid) {
          // let { username, password, repassword } = this.loginForm
          let res = await regUser(this.loginForm);
          this.$message.success("注册成功");
          this.btn1 = "登录";
          this.title = "登录";
          this.btn2 = "切换为注册";
          this.loginForm = {
            username: "",
            password: "",
            repassword: "",
          };
        } else {
          this.$message.error("用户名或密码不能为空");
        }
      });
    },
    changePwdType1(){
        if(this.passwordType1 == 'password'){
            this.passwordType1 = 'text';
        }else{
            this.passwordType1 = 'password';
        }
    },
    changePwdType2(){
        if(this.passwordType2 == 'password'){
            this.passwordType2 = 'text';
        }else{
            this.passwordType2 = 'password';
        }
    },
  },
};
</script>

<style lang="scss" scoped>
$bg: #2d3a4b;
$dark_gray: #889aa4;
$light_gray: #eee;
$cursor: #fff;

.login-container {
  min-height: 100%;
  width: 100%;
  background-color: $bg;
  overflow: hidden;

  .login-form {
    position: relative;
    width: 520px;
    max-width: 100%;
    padding: 160px 35px 0;
    margin: 0 auto;
    overflow: hidden;

    ::v-deep .el-form-item {
      border: 1px solid rgba(255, 255, 255, 0.1);
      background: rgba(0, 0, 0, 0.1);
      border-radius: 5px;
      color: #454545;
    }

    ::v-deep .el-input {
      display: inline-block;
      height: 47px;
      width: 85%;

      input {
        background: transparent;
        border: 0px;
        -webkit-appearance: none;
        border-radius: 0px;
        padding: 12px 5px 12px 15px;
        color: $light_gray;
        height: 47px;
        caret-color: $cursor;
      }
    }
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    display: inline-block;
  }

  .title-container {
    position: relative;

    .title {
      font-size: 26px;
      color: $light_gray;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
}
</style>
